﻿<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.IO;
using System.Web.SessionState;
using SIS.BLL;
using SIS.ENT;

public class Handler : IHttpHandler, IReadOnlySessionState
{

    public void ProcessRequest(HttpContext context)
    {
        if (context.Request.QueryString["key"] != null && context.Request.QueryString["type"] != null)
        {
            try
            {
                context.Response.ContentType = "image/jpeg";
                string _strType = context.Request.QueryString["type"].ToString().ToLower();
                string _strKey = context.Request.QueryString["key"].ToString();

                Stream strm = null;

                if (_strType == "schoollogo")
                {
                    strm = GetSchoolLogo(Convert.ToInt32(_strKey));
                }
                else if (_strType == "schoolphoto")
                {
                    strm = GetSchoolPhoto(Convert.ToInt32(_strKey));
                }
                else if (_strType == "studentphoto")
                {
                    strm = GetStudentPhoto(Convert.ToInt32(_strKey));
                }
                else if (_strType == "teacherphoto")
                {
                    strm = GetTeacherPhoto(Convert.ToInt32(_strKey));
                }
                else if (_strType == "employeephoto")
                {
                    strm = GetEmployeePhoto(Convert.ToInt32(_strKey));
                }

                if (strm != null)
                {
                    byte[] buffer = new byte[4096];
                    int byteSeq = strm.Read(buffer, 0, 4096);

                    while (byteSeq > 0)
                    {
                        context.Response.OutputStream.Write(buffer, 0, byteSeq);
                        byteSeq = strm.Read(buffer, 0, 4096);
                    }
                }
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
        else
        {
            throw new ArgumentException("No image found!");
        }
    }

    public Stream GetSchoolLogo(int SchoolKey)
    {
        try
        {
            Byte[] imgLogo = null;
            imgLogo = SchoolProfileTableManager.GetLogo(SchoolKey);
            return new MemoryStream((byte[])imgLogo);
        }
        catch
        {
            return null;
        }
    }

    public Stream GetSchoolPhoto(int SchoolKey)
    {
        try
        {
            Byte[] imgPhoto = null;
            imgPhoto = SchoolProfileTableManager.GetPhoto(SchoolKey);
            return new MemoryStream((byte[])imgPhoto);
        }
        catch
        {
            return null;
        }
    }


    public Stream GetStudentPhoto(int StudentProfileKey)
    {
        try
        {
            Byte[] imgPhoto = null;
            imgPhoto = StudentProfileTableManager.GetPhoto(StudentProfileKey);
            return new MemoryStream((byte[])imgPhoto);
        }
        catch
        {
            return null;
        }
    }

    public Stream GetTeacherPhoto(int TeacherProfileKey)
    {
        try
        {
            Byte[] imgPhoto = null;
            imgPhoto = TeacherProfileTableManager.GetPhoto(TeacherProfileKey);
            return new MemoryStream((byte[])imgPhoto);
        }
        catch
        {
            return null;
        }
    }

    public Stream GetEmployeePhoto(int EmployeeProfileKey)
    {
        try
        {
            Byte[] imgPhoto = null;
            imgPhoto = EmployeeProfileTableManager.GetPhoto(EmployeeProfileKey);
            return new MemoryStream((byte[])imgPhoto);
        }
        catch
        {
            return null;
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}